var graph = argument0;
var visited = 0;
var height = array_height_2d(graph);
var width = array_length_2d(graph, 0);

var stack = ds_stack_create();
var vertices = ds_list_create();

var dir = -1;
var start = 0;

for (var iii = 0; iii < height; iii++)
{
    for (var jjj = 0; jjj < width; jjj++)
    {
        visited[iii, jjj] = 0;
        if (graph[iii, jjj] == 2)
        {
            start[0] = iii;
            start[1] = jjj;
            visited[iii, jjj] = 1;
        }
    }
}
ds_stack_push(stack, start);

while(ds_stack_size(stack) != 0)
{
    var current = 0;
    current = ds_stack_pop(stack);
    var neighbor0 = 0;
    neighbor0[0] = current[0];
    neighbor0[1] = current[1] + 1;
    if(neighbor0[1] < width && visited[neighbor0[0], neighbor0[1]] == 0 && graph[neighbor0[0], neighbor0[1]] != 0)
    {
        if(dir != 0)
        {
            if(graph[current[0], current[1]] == 1) 
            {
                ds_list_add(vertices, current); 
            }
            dir = 0;
        }
        visited[neighbor0[0], neighbor0[1]] = 1;
        ds_stack_push(stack, neighbor0);
    }
    
    var neighbor1 = 0;
    neighbor1[0] = current[0] - 1;
    neighbor1[1] = current[1];
    if(neighbor1[0] > -1 && visited[neighbor1[0], neighbor1[1]] == 0 && graph[neighbor1[0], neighbor1[1]] != 0)
    {
        if(dir != 1)
        {
            if(graph[current[0], current[1]] == 1) 
            {
                ds_list_add(vertices, current); 
            }
            dir = 1;
        }
        visited[neighbor1[0], neighbor1[1]] = 1;
        ds_stack_push(stack, neighbor1);
    }
    
    var neighbor2 = 0;
    neighbor2[0] = current[0];
    neighbor2[1] = current[1] - 1;
    if(neighbor2[1] > -1 && visited[neighbor2[0], neighbor2[1]] == 0 && graph[neighbor2[0], neighbor2[1]] != 0)
    {
        if(dir != 2)
        {
            if(graph[current[0], current[1]] == 1) 
            {
                ds_list_add(vertices, current); 
            }
            dir = 2;
        }
        visited[neighbor2[0], neighbor2[1]] = 1;
        ds_stack_push(stack, neighbor2);
    }
    
    var neighbor3 = 0;
    neighbor3[0] = current[0] + 1;
    neighbor3[1] = current[1];
    if(neighbor3[0] < height && visited[neighbor3[0], neighbor3[1]] == 0 && graph[neighbor3[0], neighbor3[1]] != 0)
    {
        if(dir != 3)
        {
            if(graph[current[0], current[1]] == 1) 
            {
                ds_list_add(vertices, current); 
            }
            dir = 3;
        }
        visited[neighbor3[0], neighbor3[1]] = 1;
        ds_stack_push(stack, neighbor3);
    }   
}


for(var iii = 0; iii < ds_list_size(vertices); iii++)
{
    var toMark = 0;
    toMark = ds_list_find_value(vertices, iii);
    if(irandom(100) > 20 && 
        toMark[1] + 1 < width && graph[toMark[0], toMark[1] + 1] != 2 &&
        toMark[1] + 1 < width && graph[toMark[0], toMark[1] + 1] != 3 &&
        toMark[1] + 1 < width && graph[toMark[0], toMark[1] + 1] != 4 &&
        
        toMark[0] - 1 > -1 && graph[toMark[0] - 1, toMark[1]] != 2 &&
        toMark[0] - 1 > -1 && graph[toMark[0] - 1, toMark[1]] != 3 &&
        toMark[0] - 1 > -1 && graph[toMark[0] - 1, toMark[1]] != 4 &&
        
        toMark[1] - 1 > -1 && graph[toMark[0], toMark[1] - 1] != 2 &&
        toMark[1] - 1 > -1 && graph[toMark[0], toMark[1] - 1] != 3 &&
        toMark[1] - 1 > -1 && graph[toMark[0], toMark[1] - 1] != 4 &&
        
        toMark[0] + 1 < height && graph[toMark[0] + 1, toMark[1]] != 2 &&
        toMark[0] + 1 < height && graph[toMark[0] + 1, toMark[1]] != 3 &&
        toMark[0] + 1 < height && graph[toMark[0] + 1, toMark[1]] != 4) 
    {
        graph[toMark[0], toMark[1]] = 4;
    }
    else
    {
        graph[toMark[0], toMark[1]] = 5;
    }
}
ds_stack_destroy(stack);
ds_list_destroy(vertices);
return graph;
